//
//  MyCenterCell.m
//  GuShiNews
//
//  Created by huachun on 2019/8/16.
//  Copyright © 2019 Jones. All rights reserved.
//

#import "MyCenterCell.h"
#import "CenterModel.h"
#import "OSSTestMacros.h"
#import "HiJonesClass.h"

@interface MyCenterCell ()

@property (nonatomic, strong) UIImageView *Img;
@property (nonatomic, strong) UILabel     *Name;
@property (nonatomic, strong) UILabel     *Status;
@property (nonatomic, strong) UIButton    *User_sex;
@property (nonatomic, strong) UILabel     *Yuan;

@end

@implementation MyCenterCell

+ (instancetype)cellForTableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *ID = @"MyCenterCell";
    MyCenterCell  *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell){
        cell=[[MyCenterCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        cell.backgroundColor=[UIColor whiteColor];
        tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
        cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
        [cell setSelectionStyle:UITableViewCellSelectionStyleDefault];
        
        
        [cell.contentView addSubview:cell.Img];
        [cell.contentView addSubview:cell.Name];
        [cell.contentView addSubview:cell.Status];
        [cell.contentView addSubview:cell.User_sex];
        [cell.contentView addSubview:cell.Yuan];
    }
    
    return cell;
}

- (void)setModel:(CenterModel *)Model{
    if (_Model != Model) {
        _Model = Model;
    }
    
    UserModel *userlin = [UserModel info];
    
    _Yuan.hidden = YES;

    self.User_sex.hidden=YES;
    if ([_Model.Type isEqualToString:@"10"]) {
        self.Img.hidden  = NO;
        self.Img.frame = CGRectMake(RECRWITCH/2-35, 25, 70, 70);
        self.Img.layer.cornerRadius = 35;
        
        if (userlin.userid) {
            [self.Img sd_setImageWithURL:[NSURL URLWithString:userlin.logo] placeholderImage:[UIImage imageNamed:@"user_logo.png"]];
            _Img.contentMode = UIViewContentModeScaleAspectFill;
            self.Name.hidden = NO;
            self.Name.text=userlin.name;
            
            self.User_sex.hidden=NO;
            if ([userlin.sex isEqualToString:@"False"]) {
                _User_sex.backgroundColor=Bluecolor;
                [_User_sex setImage:[UIImage imageNamed:@"sex_nan"] forState:UIControlStateNormal];
            }else{
                _User_sex.backgroundColor=Fengcolor;
                [_User_sex setImage:[UIImage imageNamed:@"sex_nv"] forState:UIControlStateNormal];
            }
            [_User_sex setTitle:[NSString stringWithFormat:@" %li",(long)[HiJonesClass ageWithDateOfBirth:userlin.briday]] forState:UIControlStateNormal];
        }else{
            self.Img.image = [UIImage imageNamed:@"user_logo.png"];
            _Img.contentMode = UIViewContentModeScaleAspectFill;
            self.Name.hidden = NO;
            self.Name.text=@"请登录";
            self.User_sex.hidden=YES;
        }
        
    }else{
        self.Img.hidden  = YES;
        self.Name.hidden = YES;
        if ([_Model.Type isEqualToString:@"4"]){
            self.Img.hidden  = NO;
            self.Img.frame = CGRectMake(RECRWITCH-115, 15, 30, 30);
            self.Img.layer.cornerRadius = 4;
            [self.Img sd_setImageWithURL:[NSURL URLWithString:@"https://gss1.bdstatic.com/9vo3dSag_xI4khGkpoWK1HF6hhy/baike/w%3D268%3Bg%3D0/sign=fd760c66eccd7b89e96c3d85371f259a/0dd7912397dda1448445601eb8b7d0a20df486c9.jpg"] placeholderImage:[UIImage imageNamed:@"imgmoren.png"]];
        }else if ([_Model.Type isEqualToString:@"7"]){
            
        }else if ([_Model.Type isEqualToString:@"120"]){
            self.Status.hidden=NO;
            self.Status.text=@"已认证";
            self.Status.frame =CGRectMake(RECRWITCH-100, 20, 70, 20);
            self.Status.textColor = [UIColor grayColor];
            
            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                NSUInteger totalSize      = [[SDImageCache sharedImageCache] getSize];
                dispatch_async(dispatch_get_main_queue(), ^{
                    self.Status.text = [NSString stringWithFormat:@"%.2fMB",totalSize / (1024 * 1024.0)];
                });
            });
        }else if ([_Model.Type isEqualToString:@"8"]){
            if ([DEFAULTS objectForKey:Comment_Count_key]) {
                if ([[DEFAULTS objectForKey:Comment_Count_key] intValue]>0) {
                    _Yuan.hidden = NO;
                    _Yuan.text = [NSString stringWithFormat:@"%i",[[DEFAULTS objectForKey:Comment_Count_key] intValue]];
                }
            }
        }
    }
    self.textLabel.text  = _Model.Tittle;
    if (_Model.Logo) {
        self.imageView.image = [UIImage imageNamed:_Model.Logo];
    }
    if ([_Model.Type isEqualToString:@"10"]){
        self.imageView.image =  nil;
    }
}

- (UILabel *)Yuan{
    if (!_Yuan) {
        _Yuan = [[UILabel alloc] initWithFrame:CGRectMake(RECRWITCH-60, 20, 20, 20)];
        [_Yuan setFont:[UIFont systemFontOfSize:12]];
        _Yuan.textAlignment=NSTextAlignmentCenter;
        [_Yuan setTextColor:[UIColor whiteColor]];
        _Yuan.backgroundColor=[UIColor redColor];
        _Yuan.layer.cornerRadius = 10;
        _Yuan.layer.masksToBounds=YES;
        _Yuan.layer.zPosition = 100;
    }
    return _Yuan;
}

-(UIImageView *)Img{
    if (!_Img) {
        _Img                     = [[UIImageView alloc] initWithFrame:CGRectMake(17.5, 22.5, 65, 65)];
        _Img.layer.masksToBounds = YES;
        _Img.backgroundColor = backColor;
        _Img.layer.zPosition     = 102;
        _Img.contentMode = UIViewContentModeScaleAspectFill;
    }
    return _Img;
}

-(UILabel *)Name{
    if (!_Name) {
        _Name = [[UILabel alloc] initWithFrame:CGRectMake(20, 110, RECRWITCH-40, 30)];
        [_Name setFont:[UIFont boldSystemFontOfSize:16]];
        _Name.textAlignment=NSTextAlignmentCenter;
        [_Name setTextColor:[JonesClass stringTOColor:@"#222222"]];
    }
    return _Name;
}

-(UILabel *)Status{
    if (!_Status) {
        _Status = [[UILabel alloc] initWithFrame:CGRectMake(RECRWITCH-100, 20, 70, 20)];
        [_Status setFont:[UIFont boldSystemFontOfSize:14]];
        _Status.layer.zPosition = 100;
        _Status.textAlignment=NSTextAlignmentRight;
        [_Status setTextColor:NavItemTitleColor];
    }
    return _Status;
}

- (UIButton *)User_sex {
    if (!_User_sex) {
        _User_sex = [[UIButton alloc] initWithFrame:CGRectMake(RECRWITCH/2-25, 155, 50, 16)];
        [_User_sex setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _User_sex.titleLabel.font=[UIFont boldSystemFontOfSize:12];
        _User_sex.layer.cornerRadius=8;
    }
    return _User_sex;
}

@end
